#include<iostream>
using namespace std;



int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };
bool check[100][100];
int M, N, cnt,sum;
void dfs(int i, int j)
{
    sum++;
    check[i][j] = true;
    for (int k = 0; k < 4; k++)
    {
        int x = i + dx[k], y = j + dy[k];
        if (x >= 0 && x < M && y >= 0 && y < N && !check[x][y] && x + y <= cnt)
        {
            dfs(x, y);
        }
    }
}

int wardrobeFinishing(int m, int n, int cnt) {
    M = m, N = n, cnt = cnt;
    dfs(0, 0);
    return sum;
}

int main()
{
    wardrobeFinishing(4, 7, 5);

    return 0;
}



class Solution {
public:
    int count;
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    bool check[100][100];
    int M, N, CNT;
    int wardrobeFinishing(int m, int n, int cnt) {
        M = m, N = n, CNT = cnt;
        dfs(0, 0);
        return count;
    }


    void dfs(int i, int j)
    {
        ++count;
        check[i][j] = true;
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < M && y >= 0 && y < N && x < 10 && y < 10 && !check[x][y] && x + y <= CNT)
            {
                dfs(x, y);
            }
            else if (x >= 0 && x < M && y >= 0 && y < N && !check[x][y] && dight(x, y))
            {
                dfs(x, y);
            }
        }
    }

    bool dight(int i, int j)
    {
        int tmp = 0;
        while (i)
        {
            tmp += i % 10;
            i /= 10;
        }
        while (j)
        {
            tmp += j % 10;
            j /= 10;
        }

        return tmp <= CNT;
    }
};